nition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    // 迭代法 我们首先处理完毕所有的左节点
    // 左节点为空之后 开始处理右节点 
    vector<int> inorderTraversal(TreeNode* root) 
    {
        stack<TreeNode*> st;
        vector<int> ans;
        TreeNode* cur = root;

        // 遍历
        while(cur != nullptr || !st.empty())
        {
            // 首先处理左
            if (cur)
            {
                st.push(cur);
                cur = cur -> left;
            }
            else // 处理中和右
            {
                cur = st.top();
                st.pop();
                ans.push_back(cur->val);
                cur = cur -> right;
            }
        }

        return ans;
    }
};
